package info.ensa.googlemapsandroidv2;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class ItineraireProcheActivity extends Activity {

	String longitude;
	double longi;
	String latitude;
	double lati;
	String max;
	int maximum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_itineraire_proche);
		max = getIntent().getStringExtra("Max");
		Toast.makeText(getApplicationContext(), "Max= " + max,
				Toast.LENGTH_LONG).show();
		maximum = Integer.parseInt(max);
		Log.i("Max: ", max);

		double malongi = -7.999141;
		double malati = 31.6641081;
		double distance;
		double distancemin;
		double longitudemin;
		double latitudemin;

		Location here = new Location("Current");
		Location dest = new Location("Destination");

		ArrayList<Double> listelongitude = new ArrayList<Double>();
		ArrayList<Double> listelatitude = new ArrayList<Double>();
		ArrayList<Double> listedistance = new ArrayList<Double>();

		for (int j = 0; j < maximum; j++) {
			longitude = getIntent().getStringExtra("Longitude" + j);
			longi = Double.parseDouble(longitude);
			Log.i("Ma longitude" + j, "" + longi);
			Toast.makeText(getApplicationContext(),
					"Longitude" + j + "= " + longitude, Toast.LENGTH_LONG)
					.show();
			listelongitude.add(longi);

			latitude = getIntent().getStringExtra("Latitude" + j);
			lati = Double.parseDouble(latitude);
			Log.i("Ma latitude" + j, "" + lati);
			Toast.makeText(getApplicationContext(),
					"Latitude" + j + "= " + lati, Toast.LENGTH_LONG).show();
			listelatitude.add(lati);

			here.setLatitude(malati);
			here.setLongitude(malongi);
			dest.setLatitude(lati);
			dest.setLongitude(longi);
			distance = here.distanceTo(dest);
			Log.i("Ma distance", "" + distance);
			listedistance.add(distance);

		}
		distancemin = listedistance.get(maximum - 1);
		longitudemin = listelongitude.get(maximum - 1);
		latitudemin = listelatitude.get(maximum - 1);

		for (int i = 0; i < listedistance.size(); i++) {
			if (listedistance.get(i) < distancemin) {
				distancemin = listedistance.get(i);
				longitudemin = listelongitude.get(i);
				latitudemin = listelatitude.get(i);
			}
		}
		
		Log.i("Distance Min", ""+distancemin);
		Log.i("Longitude Min", ""+longitudemin);
		Log.i("Latitude Min", ""+latitudemin);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.itineraire_proche, menu);
		return true;
	}

}
